﻿using System.Globalization;
using EasyI18n.Api.Engines.Bases;
using EasyI18n.Api.Infrastructure.CorsFunction;
using EasyI18n.Api.Infrastructure.JwtFunction;
using FluentValidation;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;

namespace EasyI18n.Api.Engines.ConventionEngines;

/// <summary>
///     一些常规的配置
/// </summary>
public class ConfigureConvention : IBuilderEngine
{
    private readonly IServiceCollection services;

    public ConfigureConvention(IServiceCollection services)
    {
        this.services = services;
    }

    public void Run()
    {
        services.AddHttpClient();
        services.AddHttpContextAccessor();
        services.AddControllers().AddJsonOptions(options =>
        {
            options.JsonSerializerOptions.Converters.Add(new Rfc1123DateTimeConverter());
            options.JsonSerializerOptions.Converters.Add(new Rfc1123DateTimeOffsetConverter());
        });
        services.AddRouting(e => { e.LowercaseUrls = true; });
        services.AddEndpointsApiExplorer();
        services.AddCorsFunction();
        services.AddJwtFunction();
        services.AddExceptionHandler<GlobalExceptionHandler>();
        services.AddSwaggerGen(options => { options.DescribeAllParametersInCamelCase(); });
        services.AddMemoryCache();
    }
}

public class UseConvention : IAppEngine
{
    private readonly WebApplication app;

    public UseConvention(WebApplication app)
    {
        this.app = app;
    }

    public void Run()
    {
        app.UseHttpsRedirection();
        app.MapControllers();
        app.UseCors();
        app.UseAuthentication();
        app.UseAuthorization();
        app.MapFallbackToFile("/index.html");
    }
}